 <?php
	include_once "citation_list_functions.php";
$link = mysql_connect("localhost","studysag_dbecs","a!Ll%nM3wr)k[q")
   or die("could not connect to  MYSQL ");
  
mysql_select_db("studysag_dbecs",$link);
  
	@session_start();
	$UserID = trim($_SESSION['user_id']);
	if($UserID == "")
	{
		$UserID = session_id();		
	}
	//$UserID = isset($_POST['userid']) ? $_POST['userid'] : 0;
  ?>
	<div id="reference">  
	<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" class="borders" valign="top">
	<?php
	$tr = isset($_GET['tr']) ? intval($_GET['tr']) : 0;
	if($tr == 0) {
		$tr = $title_reference_id;
		if($tr == "") {
			$tr = isset($_SESSION['tr_s']) ? intval($_SESSION['tr_s']) : $tr;
		}
	}
	$citationList = "";
	$citationToInsert = "";
	$citationOptions = "";
	$index = 1;
	$query = "select * from composedocument where username='".$UserID."' and types!='compose' and title_reference='".$tr."' order by booktitle"; // and title_reference='".$title_reference_id."'
	
	$list = mysql_query($query);
	while($work = mysql_fetch_array($list)){	
	?>
      <tr>
        <td width="9%" align="center" valign="top" style="padding-top:10px;" class="font12">&bull;</td>
        <td width="79%" align="left" valign="middle" class="font12" style="font-size:11px;">
	<?php
		$all_name = "";
			$all_name = $work['firstname'] . "`" . $work['lastname'];
			if ($work['author'] != "")
				$all_name .= "," . $work['author'];
			$exp = explode(",",$all_name);			
			$count = count($exp);
			$result = "";			
			$year = doubleQuoteJS($work['pyear']);
			if ($work['types'] == 'book')
			{				
				$lastNameJS = "";	
				$citationOptions = "";
				
				if ($count == 1)
				{				
					$lastName = getLastName($exp[0]);
					$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
					$lastNameJS = doubleQuoteJS($lastName);										
					
					$citationOptions = $lastNameJS . '(' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					$citationOptions = "(" . $lastNameJS . ',' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ',';
				}
				else
				{
					$tempCitation = "";
					for($i = 0; $i < $count; $i++)
					{
						$lastName = getLastName($exp[$i]);					
						$tempLastNameJS = doubleQuoteJS($lastName);
						if( $i == $count - 1)
						{
							if ($count < 7)
							{
								$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							else
							{
								$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							
							if ($count < 3)
							{							
								$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
								
								$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ',p.)';
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
								
								$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ',p.)';
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
							}
						}
						else
						{
							$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
							if ($count < 3)
							{
								$tempCitation .= $tempLastNameJS . ", ";
							}
						}						
					}
				}			
				
				if ($count >= 3)
				{				
					$lastName = getLastName($exp[0]);						
					$citationOptions = $lastName . ' et al (' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					
					$citationOptions = '(' . $lastName . ' et al, ' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
				}
				if ($work['edition'] != "")
				{
					$result .= "(" . $year . "). <i>" . $work['booktitle'] . "</i>(" . 
						getFormatEdition($work['edition']) . "). " . $work['pcity'] . ": " . $work['publisher'] . ".";
				}
				else
				{
					$result .= "(" . $year . "). <i>" . $work['booktitle'] . "</i>. " . $work['pcity'] . ": " . $work['publisher'] . ".";
				}
			}
			else if ($work['types'] == 'journal')//journal
			{
				$lastNameJS = "";
				if ($count == 1)
				{	
					$lastName = getLastName($exp[0]);
					$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
					$lastNameJS = doubleQuoteJS($lastName);										
					
					$citationOptions = $lastNameJS . '(' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					$citationOptions = "(" . $lastNameJS . ',' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
				}
				else
				{
					$tempCitation = "";
					for($i = 0; $i < $count; $i++)
					{
						$lastName = getLastName($exp[$i]);					
						$tempLastNameJS = doubleQuoteJS($lastName);
						if( $i == $count - 1)
						{
							if ($count < 7)
							{
								$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							else
							{
								$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							
							if ($count < 3)
							{							
								$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
								
								$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ')';
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
								
								$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ')';	
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
							}
						}
						else
						{
							$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
							if ($count < 3)
							{
								$tempCitation .= $tempLastNameJS . ", ";
							}
						}						
					}
				}
				
				if ($count >= 3)
				{				
					$lastName = getLastName($exp[0]);						
					$citationOptions = $lastName . ' et al (' . $year . ')';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					
					$citationOptions = '(' . $lastName . ' et al, ' . $year . ')';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
				}
				
				if ($count < 6)
				{	
					$result .= "(" . $year . "). " . $work['booktitle'] . ". <i>" . $work['journaltitle'] . "," . $work['issuenumber'] . "</i>(" . 
						$work['edition'] . "), " . $work['pagenumber'] . ".";
				}
				else
				{
					$result .= "(" . $year . "). " . $work['booktitle'] . ". <i>" . $work['journaltitle'] . "," . $work['issuenumber'] . "</i>, " . 
					$work['pagenumber'] . ".";
				}
				
				if ($work['digitalobject'] != "")
				{
					$result .= " doi:" . $work['digitalobject'];
				}
			}
			else if ($work['types'] == 'web')//website
			{
				$year = doubleQuoteJS($work['website_year']);
				$month = doubleQuoteJS($work['website_month']);
				$day = doubleQuoteJS($work['website_day']);
				
				$year_accessed = doubleQuoteJS($work['website_year_accessed']);
				$month_accessed = doubleQuoteJS($work['website_month_accessed']);
				$day_accessed = doubleQuoteJS($work['website_day_accessed']);
				
				$lastNameJS = "";
				if ($count == 1)
				{	
					$lastName = getLastName($exp[0]);
					$result = $lastName . ", " . getInitialFirstName($exp[0]) . ".";
					$lastNameJS = doubleQuoteJS($lastName);										
					
					$citationOptions = $lastNameJS . ', ' . $work['name_of_webpage'] .'(' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					$citationOptions = "(" . $lastNameJS . ', ' . $work['name_of_webpage'] . ',' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
				}
				else
				{
					$tempCitation = "";
					for($i = 0; $i < $count; $i++)
					{
						$lastName = getLastName($exp[$i]);					
						$tempLastNameJS = doubleQuoteJS($lastName);
						if( $i == $count - 1)
						{
							if ($count < 7)
							{
								$result .= "&" . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							else
							{
								$result .= "..." . $lastName . ", " . getInitialFirstName($exp[$i]);
							}
							
							if ($count < 3)
							{							
								$tempCitation =  substr($tempCitation, 0, strlen($tempCitation) - 2);
								
								$citationOptions = $tempCitation . ' and ' . $tempLastNameJS . '(' . $year . ',p.)';
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
								
								$citationOptions = "(" . $tempCitation . ' & ' . $tempLastNameJS . ',' . $year . ',p.)';
								$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
								$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
							}
						}
						else
						{
							$result .= $lastName . ", " . getInitialFirstName($exp[$i]) . "., ";
							if ($count < 3)
							{
								$tempCitation .= $tempLastNameJS . ", ";
							}
						}						
					}
				}
				
				if ($count >= 3)
				{				
					$lastName = getLastName($exp[0]);						
					$citationOptions = $lastName . ' et al (' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
					
					$citationOptions = '(' . $lastName . ' et al, ' . $year . ',p.)';
					$citationList .= '"citation_' . $index++ . '":"' . $citationOptions . '",';
					$citationToInsert .= str_replace(",", "_S_S_", $citationOptions) . ",";
				}
				
				if ($count < 6)
				{	
					$result .= "(" . $year . "," . $month . " " . $day . ")<i>. de. Retrieved</i>. " . 
						$day_accessed . " " . $month_accessed . ", " . $year_accessed ."), from " . $work['name_of_webpage'] . ": ". $work['website_url'] . ".";
				}
				else
				{
					$result .= "(" . $year . "," . $month . " " . $day . ")<i>. de. Retrieved</i>. " . 
						$day_accessed . " " . $month_accessed . ", " . $year_accessed ."), from " . $work['name_of_webpage'] . ": ". $work['website_url'] . ".";
				}
						
			}
		echo $result;
		?>
		
		</td>
        <td width="12%" align="center" valign="middle" class="font12" style="font-size:11px;">
        <a href="index.php?page=assignment_manager&_e=<?php echo $work['id'];?>&_t=<?php echo $work['types']?>&tr=<?php echo $tr; ?>">
        <img src="./images/edit.jpg" border="0" title="EDIT" alt="EDIT" /></a><br /><br />
        <a href="javascript:void(0)" onclick="Del_Refernce(this,'<?php echo $work['id'];?>');">
        <img src="./images/delete.jpg" border="0" title="DELETE" alt="DELETE" /></a>
        </td>
      </tr> 
		<tr>
			<td colspan="3" style="padding:3px 0px;">
				<div style="border-bottom:solid 1px #E4E4E4;height:1px"></div>
			</td>
		</tr>
		<?php
	}
?> 
	</table>
    <?php if($tr !=0) { ?>
	<input type="hidden" name="flag" id="flag" value="1" />
    <?php } ?>
    <input type="hidden" name="tr_a" id="tr_a" value="<?php echo $tr; ?>" />
    </div>
	<?php
		echo "<script type='text/javascript'>var citationList  = [{" . substr($citationList, 0, strlen($citationList) - 1) . "}];" .
			'var citationToInsert ="' . substr($citationToInsert, 0, strlen($citationToInsert) - 1) . '";</script>';
	?>
        